package kr.or.kosta.otr.service.main;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

import javax.annotation.Resource;

import kr.or.kosta.otr.persistence.company.CompanyDao;
import kr.or.kosta.otr.persistence.companyreview.CompanyReviewDao;
import kr.or.kosta.otr.persistence.notice.NoticeDao;
import kr.or.kosta.otr.persistence.score.ScoreDao;
import kr.or.kosta.otr.service.companyservice.CompanyService;
import kr.or.kosta.otr.utill.ValueComparator;
import kr.or.kosta.otr.vo.CompanyReviewVO;
import kr.or.kosta.otr.vo.CompanyVO;
import kr.or.kosta.otr.vo.NoticeVO;

import org.springframework.stereotype.Service;

@Service("MainService")
public class MainServiceImpl implements MainService{
	@Resource(name="CompanyDao")
	private CompanyDao companyDao;

	@Resource(name="CompanyReviewDao")
	private CompanyReviewDao companyReviewDao;
	
	@Resource(name="ScoreDao")
	private ScoreDao scoreDao;

	@Resource(name = "CompanyService")
	CompanyService companyService;
	
	@Resource(name="NoticeDao")
	private NoticeDao noticeDao;
	
	@Override
	public HashMap<String, Object> getMainCompanies() throws SQLException {
		HashMap<String, Object> mainMap=new HashMap<String, Object>();
		//System.out.println("getMainCompanies start");
		//1. 평점 1위 기업
		int cid1=companyDao.getTopScoreCompanyId();
		CompanyVO cvo1=companyDao.getSimpleCompany(cid1);
		mainMap.put("cvo1", cvo1);
		//System.out.println("1");
		//2. 연봉 1위 기업
		int cid2=companyReviewDao.getTopSalaryCompanyId();
		CompanyVO cvo2=companyDao.getSimpleCompany(cid2);
		mainMap.put("cvo2", cvo2);
		//System.out.println("2");
		
		//3. 리뷰수 1위 기업
		int cid3=companyReviewDao.getHotCompanyId();
		CompanyVO cvo3=companyDao.getSimpleCompany(cid3);
		mainMap.put("cvo3", cvo3);
		//System.out.println("3");
		
		//4. 최신 리뷰가 작성된 기업
		int cid4=companyReviewDao.getRecentReviewedCompanyId();
		CompanyVO cvo4=companyDao.getSimpleCompany(cid4);
		mainMap.put("cvo4", cvo4);
		//System.out.println("4");
		
		//5. 휴가자율성 1위 기업
		int cid5=getTopHolidayCompanyId();
		CompanyVO cvo5=companyDao.getSimpleCompany(cid5);
		mainMap.put("cvo5", cvo5);
		//System.out.println("5");
		
		//6. 복지 1위 기업
		int cid6=getTopWelfareCompanyId();
		CompanyVO cvo6=companyDao.getSimpleCompany(cid6);
		mainMap.put("cvo6", cvo6);
		//System.out.println("6");
		
		//7. 초과근무 1위 기업
		int cid7=getTopOverdueCompanyId();
		CompanyVO cvo7=companyDao.getSimpleCompany(cid7);
		mainMap.put("cvo7", cvo7);
		//System.out.println("7");
		
		//8. 서울 1위 기업
		int cid8=companyDao.getTopCompanyIdInRegion();
		CompanyVO cvo8=companyDao.getSimpleCompany(cid8);
		mainMap.put("cvo8", cvo8);
		//System.out.println("8");
		
		//9. IT산업 1위 기업
		int cid9=companyDao.getTopCompanyIdInIndustry();
		CompanyVO cvo9=companyDao.getSimpleCompany(cid9);
		mainMap.put("cvo9", cvo9);
		//System.out.println("9");
		//System.out.println("getMainCompanies end");
		return mainMap;
	}

	public int getTopHolidayCompanyId() throws SQLException{
		ArrayList<CompanyReviewVO> crList = companyService.getAllCompanyReview();

		HashMap<Integer, Float> map = new HashMap<Integer, Float>();
		for (int i = 0; i < crList.size(); i++) {
			int companyId = crList.get(i).getCompanyVo().getCompanyId();
			float scoreChance = companyService.getTotalScoreOfHoliday(companyId);
			map.put(companyId, scoreChance);
		}

		// 이 숫자 나열하기
		ValueComparator vc = new ValueComparator(map);
		TreeMap<Integer, Float> sortMap = new TreeMap<Integer, Float>(vc);

		sortMap.putAll(map);


		// company 입력하기
		ArrayList<CompanyVO> clist = new ArrayList<CompanyVO>();
		for (Integer companyId : sortMap.keySet()) {
			clist.add(companyService.getOneCompany(companyId));
		}
		
		int cid=0;
		for(int i=0; i<1; i++){
			cid=clist.get(i).getCompanyId();
		}
		return cid;
	}
	
	public int getTopWelfareCompanyId() throws SQLException{
		ArrayList<CompanyReviewVO> crList = companyService.getAllCompanyReview();

		HashMap<Integer, Float> map = new HashMap<Integer, Float>();
		for (int i = 0; i < crList.size(); i++) {
			int companyId = crList.get(i).getCompanyVo().getCompanyId();
			float scoreChance = companyService.getTotalScoreOfWelfare(companyId);
			map.put(companyId, scoreChance);
		}

		// 이 숫자 나열하기
		ValueComparator vc = new ValueComparator(map);
		TreeMap<Integer, Float> sortMap = new TreeMap<Integer, Float>(vc);

		sortMap.putAll(map);


		// company 입력하기
		ArrayList<CompanyVO> clist = new ArrayList<CompanyVO>();
		for (Integer companyId : sortMap.keySet()) {
			clist.add(companyService.getOneCompany(companyId));
		}
		
		int cid=0;
		for(int i=0; i<1; i++){
			cid=clist.get(i).getCompanyId();
		}
		return cid;
	}
	
	public int getTopOverdueCompanyId() throws SQLException{
		ArrayList<CompanyReviewVO> crList = companyService.getAllCompanyReview();

		HashMap<Integer, Float> map = new HashMap<Integer, Float>();
		for (int i = 0; i < crList.size(); i++) {
			int companyId = crList.get(i).getCompanyVo().getCompanyId();
			float scoreChance = companyService.getTotalScoreOfOverdue(companyId);
			map.put(companyId, scoreChance);
		}

		// 이 숫자 나열하기
		ValueComparator vc = new ValueComparator(map);
		TreeMap<Integer, Float> sortMap = new TreeMap<Integer, Float>(vc);

		sortMap.putAll(map);


		// company 입력하기
		ArrayList<CompanyVO> clist = new ArrayList<CompanyVO>();
		for (Integer companyId : sortMap.keySet()) {
			clist.add(companyService.getOneCompany(companyId));
		}
		
		int cid=0;
		for(int i=0; i<1; i++){
			cid=clist.get(i).getCompanyId();
		}
		return cid;
	}
	
	@Override
	public NoticeVO getMainNotice() throws SQLException {
		return noticeDao.getRecentNotice();
	}
}
